<?php
namespace Admin\Controller;

class GoodsController extends CommonController{
 	/*整车列表*/
    public function index(){
        $this->where = array();
        $this->where['type'] = '1';
        $this->where['delete_status'] = '1';
        I('get.keywords') && $this->where['goods_name'] = array('like','%'.I('get.keywords').'%');
        $this->order = 'create_time desc';
        parent::index();
    }
    
    /*配件列表*/
    public function part(){
        $this->where = array();
        $this->where['type'] = '2';
        $this->where['delete_status'] = '1';
        I('get.keywords') && $this->where['goods_name'] = array('like','%'.I('get.keywords').'%');
        $this->order = 'create_time desc';
        parent::index();
    }
    
    /*添加商品*/
    public function add_goods(){
    	$currentModel = D(CONTROLLER_NAME);
    	if(IS_POST){
    		if(!$data = $currentModel->get_form()) $this->error($currentModel->getError());
    		$specs = $data['spec'];
    		unset($data['spec']);
    		$res = $currentModel->saveData($data);
    		foreach ($specs as $val){
    			$val['goods_id'] = $data['id'] ? $data['id'] : $res;
    			D('GoodsSpec')->saveData($val);
    		}
    		$res === false ? $this->error('保存失败') : $this->success('保存成功',U('index'));
    	}else{
    		$formData = array();
    		I('get.id') && $formData = $currentModel->relation(true)->where(array('id'=>(int)I('get.id')))->find();
    		$this->assign('formData',output_data($formData));
    		$this->display();
    	}
    }
    
    /*添加配件*/
    public function add_part(){
    	$currentModel = D(CONTROLLER_NAME);
    	if(IS_POST){
    		if(!$data = $currentModel->get_form()) $this->error($currentModel->getError());
    		$res = $currentModel->saveData($data);
    		$res === false ? $this->error('保存失败') : $this->success('保存成功',U('part'));
    	}else{
    		$formData = array();
    		I('get.id') && $formData = $currentModel->where(array('id'=>(int)I('get.id')))->find();
    		$this->assign('formData',output_data($formData));
    		$this->display();
    	}
    }
    
	/*删除数据*/
    public function dele_data(){
    	$result = D(CONTROLLER_NAME)->where(array('id'=>intval(I('post.id'))))->save(array('delete_status'=>'2'));
    	empty($result)?$this->error():$this->success('删除成功！');
    }
    
    /*修改 上/下架 状态*/
    public function set_status(){
    	$result = D(CONTROLLER_NAME)->where(array('id'=>intval(I('post.id'))))->save(array('status'=>I('post.status')));
    	empty($result)?$this->error():$this->success();
    }
}
